/* This class represents a sector which is a group of nodes
 * 
 */

package map;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Sector implements Serializable, Comparable<Sector>{

	private static final long serialVersionUID = 845624981513412298L;
	private static final int SECTOR_ROWS = 9; //the number of rows int the sector
	private static final int SECTOR_COLS = 16; //the number of the columns in the sector
	private static final int NUM_NODES = 20;
	private int row; //the row of the galaxy it is in
	private int col; //the col of the galaxy it is in
	private ArrayList<Node> nodes; //the list of nodes in the sector
	private int level; //the "level" of the sector. All nodes in this sector will be of the same level
	private char[][] sector; //holds a representation of the nodes

	
	//2 param constructor that makes an empty sector
	public Sector(int r, int c){
		
		row = r;
		col = c;
	}//int int constructor

	//generates the Sector
	public void generate(int l){

		level = l;
		nodes = new ArrayList<Node>(NUM_NODES);

		for(int i = 0; i < NUM_NODES; i++)
			nodes.add(null);
		
		sector = new char[SECTOR_ROWS][SECTOR_COLS];
		Random r = new Random(); //used for generating the locations of the nodes

		//start off by setting the sector to an empty sector
		for(int i = 0; i < SECTOR_ROWS; i++){
			for(int j = 0; j < SECTOR_COLS; j++){

				sector[i][j] = '-';
			}//for
		}//for

		//insert the Entrance and Exit nodes
		int row = r.nextInt(SECTOR_ROWS / 2 + 1) + SECTOR_ROWS / 4; //used to hold randomly generated rows
		int col = r.nextInt(SECTOR_ROWS / 2 + 1) + SECTOR_ROWS / 4; //used to hold randomly generated cols
		sector[row][0] = 'S';
		sector[col][SECTOR_COLS - 1] = 'E'; //I use col here because I need two random numbers and needed to keep track of both

		nodes.set(0, new Node(row, 0, level, EventType.FUELING_STATION));
		nodes.set(NUM_NODES - 1, new Node(col, SECTOR_COLS - 1, level, EventType.random()));

		//generates 8 non "door" nodes. These nodes cannot be in the same column as the entrance and exit nodes but can be anywhere else
		//nodes are also not allowed to be directly adjacent to any other node in any of the 8 directions.
		for(int i = 1; i <= NUM_NODES - 2; i++){

			boolean cont = true; //holds of another set of coordinates need to be generated
			row = 0;
			col = 0;

			//keeps on generating nodes until a valid one is found
			do{
				row = r.nextInt(SECTOR_ROWS);
				col = r.nextInt(SECTOR_COLS - 2) + 1;

				//checks to see if that space is already occupied
				while(sector[row][col] == 'N'){				

					row = r.nextInt(SECTOR_ROWS);
					col = r.nextInt(SECTOR_COLS - 2) + 1;
				}//while

				//Check all eight directions in a clockwise direction starting with left.
				if(sector[row][col - 1] == '-')
					if(row - 1 == -1 || sector[row - 1][col - 1] == '-')
						if(row - 1 == -1 || sector[row - 1][col] == '-')
							if(row - 1 == -1 || sector[row - 1][col + 1] == '-')
								if(sector[row][col + 1] == '-')
									if(row + 1 == SECTOR_ROWS || sector[row + 1][col + 1] == '-')
										if(row + 1 == SECTOR_ROWS || sector[row + 1][col] == '-')
											if(row + 1 == SECTOR_ROWS || sector[row + 1][col - 1] == '-')
												cont = false;


			}while(cont);

			//places the node after all checks are made
			sector[row][col] = 'N';
			nodes.set(i, new Node(row, col, level, EventType.random()));
		}//for

		//force up to three stores to appear in the sector
		for(int i = 0; i < 2; i++){

			nodes.get(r.nextInt(NUM_NODES - 2) + 1).setEvent(EventType.STORE);
		}//for


		//now forces a quest
		int questG = r.nextInt(NUM_NODES - 2) + 1; //the location of the quest giver
		int questM = r.nextInt(NUM_NODES - 2) + 1; //the location of the quest marker

		while(questM == questG){

			questM = r.nextInt(NUM_NODES - 2) +1;
		}//while


		nodes.get(questG).setEvent(EventType.QUEST_GIVER);
		nodes.get(questM).setEvent(EventType.QUEST_MARKER);		
		
		Collections.sort(nodes);
	}//generate

	
	//used to generate a sector with a final boss
	public void generate(int l, boolean b){
		
		this.generate(l);
	}//generate


	//returns the row
	public int getRow(){
		
		return row;
	}//getRow

	
	//returns the column
	public int getCol(){
		
		return col;
	}//getCol
	
	
	//returns a comparison of the columns
	public int compareTo(Sector s){
		
		return this.col - s.col;
	}//compareTo
	
	
	//returns a string representation of the sector
	public String toString(){

		String toReturn = "  0 1 2 3 4 5 6 7 8 9 A B C D E F\n"; //the string that is returned

		for(int i = 0; i < SECTOR_ROWS; i++){

			toReturn += i + "";

			for(int j = 0; j < SECTOR_COLS; j++){

				toReturn += " " + sector[i][j];				
			}//for

			toReturn += "\n";
		}//for		
		return toReturn;
	}//toString
//
//
//	public static void main(String[] args){
//
//		int numSectors = 100;
//		Sector[] s = new Sector[numSectors];
//		double[] battles = new double[numSectors];
//		double[] events = new double[numSectors];
//		double[] others = new double[numSectors];
//		double numBattles = 0;
//		double numEvents = 0;
//		double numOthers = 0;
//		double averageBattles = 0;
//		double averageEvents = 0;
//		double averageOthers = 0;
//		double numLowBattles = 0;
//		double numLowEvents = 0;
//
//		for(int i = 0; i < numSectors; i++){
//
//			s[i] = new Sector(0,0);
//			s[i].generate(i);
//			System.out.println(s[i]);
//			for(Node n: s[i].nodes)
//				System.out.println(n);
//		}//for
//
//		for(int i = 0; i < numSectors; i++){
//
//			for(int j = 1; j < NUM_NODES - 1; j++){
//
//				if(s[i].nodes.get(j).getEvent() == EventType.BATTLE){
//
//					battles[i]++;
//					numBattles++;
//				}//if
//
//				else if(s[i].nodes.get(j).getEvent() == EventType.RANDOM_EVENT){
//
//					events[i]++;
//					numEvents++;
//				}//else if
//
//				else {
//					
//					others[i]++;
//					numOthers++;
//				}//else
//			}//for
//		}//for
//		
//		System.out.println("\n\nAverage number of battles is: " + numBattles / numSectors);
//		System.out.println("Average number of events is: " + numEvents / numSectors);
//		System.out.println("Average number of others is: " + numOthers / numSectors);
//		
//		System.out.println("Total percentage of battles: " + numBattles / (NUM_NODES * numSectors));
//		System.out.println("Total percentage of events: " + numEvents / (NUM_NODES * numSectors));
//		System.out.println("Total percentage of others: " + numOthers / (NUM_NODES * numSectors));
//		
//		for(int i = 0; i < numSectors; i++){
//			
//			averageBattles += battles[i] / NUM_NODES;
//			averageEvents += events[i] / NUM_NODES;
//			averageOthers += others[i] / NUM_NODES;
//		}//for
//		
//		averageBattles /= numSectors;
//		averageEvents /= numSectors;
//		averageOthers /= numSectors;
//		
//		System.out.println("The average percentage of battles in a sector is: " + averageBattles);
//		System.out.println("The average percentage of events in a sector is: " + averageEvents);
//		System.out.println("The average percentage of others in a sector is: " + averageOthers);
//		
//		for(int i = 0; i < numSectors; i++){
//			
//			if(battles[i] <= 4)
//				numLowBattles++;
//			
//			if(events[i] <= 4)
//				numLowEvents++;
//		}//for
//		
//		
//		System.out.println("The number of sectors with 4 or less battles are: " + numLowBattles + " Percentage is: " + numLowBattles / numSectors);
//		System.out.print("The number of sectors with 4 or less events are: " + numLowEvents + " Percentage is: " + numLowEvents / numSectors);	
//	}//main
}//Sector